package lt.mockos.app.client.widget;

import lt.mockos.app.client.cpu.hw.type.ProcessStatus;

import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;

public class VMControls extends FlexTable implements HasValueChangeHandlers<String> {

    private static final String VM_SELECT_GROUP = "vm_select";

    private RadioButton vm1RadioButton = new RadioButton(VM_SELECT_GROUP, VM.VM1.name());
    private RadioButton vm2RadioButton = new RadioButton(VM_SELECT_GROUP, VM.VM2.name());
    private RadioButton vm3RadioButton = new RadioButton(VM_SELECT_GROUP, VM.VM3.name());
    private VMStatusIcon vm1Status = new VMStatusIcon();
    private VMStatusIcon vm2Status = new VMStatusIcon();
    private VMStatusIcon vm3Status = new VMStatusIcon();
    private TextBox selectedVM = new TextBox();

    public enum VM {
        VM1, VM2, VM3
    }

    public VMControls() {
        vm1RadioButton.setValue(true);
        setWidget(0, 0, vm1RadioButton);
        setWidget(1, 0, vm2RadioButton);
        setWidget(2, 0, vm3RadioButton);
        setWidget(3, 0, selectedVM);
        selectedVM.setVisible(false);

        vm1RadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                if (event.getValue())
                    selectedVM.setValue(VM.VM1.name(), true);

            }
        });
        vm2RadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                if (event.getValue())
                    selectedVM.setValue(VM.VM2.name(), true);

            }
        });
        vm3RadioButton.addValueChangeHandler(new ValueChangeHandler<Boolean>() {

            @Override
            public void onValueChange(ValueChangeEvent<Boolean> event) {
                if (event.getValue())
                    selectedVM.setValue(VM.VM3.name(), true);

            }
        });
    }

    public VM getSelectedVM() {
        VM vm = VM.VM1;
        if (vm1RadioButton.getValue()) {
            vm = VM.VM1;
        } else if (vm2RadioButton.getValue()) {
            vm = VM.VM2;
        } else if (vm3RadioButton.getValue()) {
            vm = VM.VM3;
        }
        return vm;
    }

    public void setStatus(VM vm, ProcessStatus status) {
        switch (vm) {
            case VM1:
                vm1Status.setStatus(status);
                break;
            case VM2:
                vm2Status.setStatus(status);
                break;
            case VM3:
                vm3Status.setStatus(status);
                break;
            default:
                break;
        }
    }

    @Override
    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
        return selectedVM.addValueChangeHandler(handler);
    }

}
